package com.kikcn.demo;

import com.kikcn.demo.domain.Resource;
import com.kikcn.demo.services.ResourceService;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;

/**
 * User: Administrator
 * Date: 2015/7/6
 * Time: 20:58
 */
public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        SecurityContext securityContext= SecurityContextHolder.getContext();
        WebApplicationContext context= WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext());
        ResourceService resourceService=(ResourceService)context.getBean("resourceService");
        List<Resource> resources=resourceService.findAll();
        servletContextEvent.getServletContext().setAttribute("resources",resources);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
